import importlib
import pkgutil
from fastapi import APIRouter
from pathlib import Path

def auto_register_routes(app, package_path: str):
    """自动注册API路由"""
    package_dir = Path(package_path).resolve()
    package_name = "app." + package_dir.name  # 添加app前缀
    
    for _, module_name, _ in pkgutil.walk_packages([str(package_dir)], prefix=package_name + "."):
        module = importlib.import_module(module_name)
        if hasattr(module, 'router') and isinstance(module.router, APIRouter):
            app.include_router(module.router)